package com.hu.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.hu.common.utils.SecurityUtils;
import com.hu.system.mapper.StockLossMapper;
import com.hu.system.mapper.ProductInfoMapper;
import com.hu.system.mapper.SysUserShopMapper;
import com.hu.system.domain.StockLoss;
import com.hu.system.domain.ProductInfo;
import com.hu.system.service.IStockLossService;

/**
 * 库存损耗Service业务层处理
 *
 * @author hu
 * @date 2025-01-30
 */
@Service
public class StockLossServiceImpl implements IStockLossService {
    @Autowired
    private StockLossMapper stockLossMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private SysUserShopMapper userShopMapper;

    /**
     * 查询库存损耗列表
     *
     * @param stockLoss 库存损耗
     * @return 库存损耗
     */
    @Override
    public List<StockLoss> selectStockLossList(StockLoss stockLoss) {
        Long userId = SecurityUtils.getUserId();

        // 管理员查看全部
        if (userId == null || !SecurityUtils.isAdmin(userId)) {
            // 保存分页参数
            com.github.pagehelper.Page<?> originalPage = com.github.pagehelper.PageHelper.getLocalPage();
            Integer originalPageNum = originalPage != null ? originalPage.getPageNum() : null;
            Integer originalPageSize = originalPage != null ? originalPage.getPageSize() : null;

            // 清除分页参数避免影响店铺查询
            PageHelper.clearPage();
            List<Long> shopIds = userShopMapper.selectShopIdsByUserId(userId);

            // 恢复分页参数
            if (originalPageNum != null && originalPageSize != null) {
                PageHelper.startPage(originalPageNum, originalPageSize);
            }

            if (shopIds == null || shopIds.isEmpty()) {
                return new ArrayList<>();
            }

            // 设置店铺ID过滤条件
            stockLoss.getParams().put("shopIdList", shopIds);
        }

        return stockLossMapper.selectStockLossList(stockLoss);
    }

    /**
     * 查询库存损耗详细
     *
     * @param lossId 损耗ID
     * @return 库存损耗
     */
    @Override
    public StockLoss selectStockLossById(Long lossId) {
        return stockLossMapper.selectStockLossById(lossId);
    }

    /**
     * 新增库存损耗
     *
     * @param stockLoss 库存损耗
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStockLoss(StockLoss stockLoss) {
        // 根据 SKU 从产品表查询完整信息
        ProductInfo product = productInfoMapper.selectProductInfoBySku(stockLoss.getSku());
        if (product != null) {
            stockLoss.setProductName(product.getProductName());
            stockLoss.setSkuAttr(product.getSkuAttr());
            stockLoss.setShopId(product.getShopId());
        }
        
        stockLoss.setCreateBy(SecurityUtils.getUsername());
        return stockLossMapper.insertStockLoss(stockLoss);
    }

    /**
     * 修改库存损耗
     *
     * @param stockLoss 库存损耗
     * @return 结果
     */
    @Override
    @Transactional
    public int updateStockLoss(StockLoss stockLoss) {
        // 根据 SKU 从产品表查询完整信息
        ProductInfo product = productInfoMapper.selectProductInfoBySku(stockLoss.getSku());
        if (product != null) {
            stockLoss.setProductName(product.getProductName());
            stockLoss.setSkuAttr(product.getSkuAttr());
            stockLoss.setShopId(product.getShopId());
        }
        
        stockLoss.setUpdateBy(SecurityUtils.getUsername());
        return stockLossMapper.updateStockLoss(stockLoss);
    }

    /**
     * 批量删除库存损耗
     *
     * @param lossIds 需要删除的库存损耗ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteStockLossByIds(Long[] lossIds) {
        return stockLossMapper.deleteStockLossByIds(lossIds);
    }

    /**
     * 删除库存损耗信息
     *
     * @param lossId 库存损耗ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteStockLossById(Long lossId) {
        return stockLossMapper.deleteStockLossById(lossId);
    }
}

